package com.nstar.uecenter.view.main.impl
{
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.util.LoadXMLDelegate;
	import com.nstar.base.util.Logger;
	import com.nstar.uecenter.view.components.ProductLoader;
	import com.nstar.uecenter.view.itemrenderer.LabIconRenderer;
	import com.nstar.uecenter.view.itemrenderer.LabRenderer;
	
	import flash.geom.Point;
	
	import mx.collections.ArrayCollection;
	import mx.containers.VBox;
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TileList;
	import mx.controls.ToggleButtonBar;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.rpc.IResponder;

	public class LabsClass extends VBox implements IResponder
	{
		public var tileList:TileList;
		
		private var loader:LoadXMLDelegate ;
		private var logger:Logger = Logger.getInstance();
		private var labs:ArrayCollection;
		
		public var tbbMode:ToggleButtonBar;
		public var lblTitle:Label;
		public var lblMode:Label;
		
		public function LabsClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			initIcons();
			loadLabs();
			tileList.addEventListener("desc",itemclickHandler);
			tbbMode.addEventListener(ItemClickEvent.ITEM_CLICK,changeStyle);
		}	
		
		private function initIcons():void{
			var icons:Array = ["icon","thumbs"];
			for(var i:int=0;i<icons.length;i++){
				Button(tbbMode.getChildAt(i)).styleName = icons[i];
			}			
		}
		
		private function itemclickHandler(event:AdvanceEvent):void{
			var p:Point = this.localToGlobal(new Point());
			ProductLoader.show(event.request,p.x,p.y,this.width,this.height);
		}	
		
		private function changeStyle(event:ItemClickEvent):void{
			switch(event.index){
				case 0:
					tileList.itemRenderer = new ClassFactory(LabRenderer);
					tileList.columnWidth = 900;
					tileList.rowHeight = 140;
					tileList.columnCount = 1;
					break;
				case 1:
					tileList.itemRenderer = new ClassFactory(LabIconRenderer);
					tileList.columnWidth = 150;
					tileList.rowHeight = 140;
					tileList.columnCount = 4;
					break;
			}
		}
		
		//加载实验产品数据
		private function loadLabs():void{
			loader = new LoadXMLDelegate(this,"demo/data/labs.xml","object");
			loader.load();
		}
		
		public function result( rpcEvent : Object ) : void
		{
			// call the helper class for parse the XML data
			labs = rpcEvent.result.labs.lab;
			addLabs(labs);
			trace("load labs ok!");
		}
		
		// this is called when the delegate receives a fault from the service
		public function fault( rpcEvent : Object ) : void 
		{
			trace("load labs error!!!");
		}
		
		//添加实验产品
		private function addLabs(labs:ArrayCollection):void{
			tileList.dataProvider = labs;
			lblTitle.text = labs.length+" 个产品和组件。";
		}
		
	}
}